package com.lanou.sfu.songforu.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.datas.RecommendRadioDatas;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import java.util.List;

/**
 * Created by quyan on 15/10/23.
 */
public class RecommendRadioAdapter extends BaseAdapter {
    private List<RecommendRadioDatas.ListEntity>datas;
    private Context context;
    private ImageLoader imageLoader;


    public RecommendRadioAdapter(Context context,List<RecommendRadioDatas.ListEntity> entityList) {
        this.context = context;
        datas = entityList;
        imageLoader = VolleySingleton.getInstance(context).getImageLoader();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_recom_radio,null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.radioImage);
            holder.descText = (TextView) convertView.findViewById(R.id.radioDesc);
            holder.nameText = (TextView) convertView.findViewById(R.id.radioName);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        RecommendRadioDatas.ListEntity listEntity = (RecommendRadioDatas.ListEntity) getItem(position);
        if (listEntity != null){
            holder.descText.setText(listEntity.getTitle());
            holder.nameText.setText(listEntity.getDesc());
            String url = listEntity.getPic();
            ImageLoader.ImageListener listener = ImageLoader.getImageListener
                    (holder.imageView,R.mipmap.default_app_rec,R.mipmap.default_app_rec);
            imageLoader.get(url,listener);
        }

        return convertView;
    }
    public class ViewHolder{
        private ImageView imageView;
        private TextView descText,nameText;
    }
}
